package com.gandong8.trace.web.controller;

import com.gandong8.trace.tool.util.ZKCuratorUtil;
import com.gandong8.trace.biz.service.DemoConsumerService;
import com.gandong8.trace.domain.vo.TestVO;
import com.gandong8.trace.tool.consumer.TwoObject;
import com.gandong8.trace.tool.domain.RequestData;
import com.gandong8.trace.tool.service.CacheService;
import com.gandong8.trace.tool.service.HttpService;
import com.gandong8.trace.tool.service.MqService;
import com.gandong8.trace.tool.util.ThreadUtil;
import com.gandong8.trace.web.domain.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.concurrent.Executor;

@Slf4j
@Controller
public class TestController{

    @Autowired(required = false) @Qualifier( "rocketMqService" )
    //@Resource(name="rocketMqService")
    private MqService rocketMq;

    @Autowired(required = false) @Qualifier( "kafkaService" )
    //@Resource(name="kafkaService")
    private MqService kafka;

    @Autowired(required = false) @Qualifier( "onsService" )
    //@Resource(name="onsService")
    private MqService ons;

    @Resource(name="myHttpService")
    private HttpService myHttp;

    @Resource(name="okHttpService")
    private HttpService okHttp;

    @Resource(name="restTemplateService")
    private HttpService restHttp;

    @Resource(name="jedisService")
    private CacheService jedis;

    @Resource(name="redissonService")
    private CacheService redisson;

    @Autowired
    private DemoConsumerService demoConsumerService;

    @RequestMapping(value = "/test")
    @ResponseBody
    public Result test() {
        log.info("test");
        return Result.success();
    }

    @RequestMapping(value = "/rocket/topic/1")
    @ResponseBody
    public Result rocketTopic1() {
        Boolean result = rocketMq.send("topic1", "test");
        return Result.success(result);
    }

    @RequestMapping(value = "/rocket/topic/2")
    @ResponseBody
    public Result rocketTopic2() {
        TwoObject obj = new TwoObject();
        obj.setContent("你好");
        obj.setTitle("test");
        Boolean result = rocketMq.send("topic2", obj);
        return Result.success(result);
    }

    @RequestMapping(value = "/kafka/topic/1")
    @ResponseBody
    public Result kafkaTopic1() {
        Boolean result = kafka.send("topic1", "test");
        return Result.success(result);
    }

    @RequestMapping(value = "/kafka/topic/2")
    @ResponseBody
    public Result kafkaTopic2() {
        TwoObject obj = new TwoObject();
        obj.setContent("你好");
        obj.setTitle("test");
        Boolean result = kafka.send("topic2", obj);
        return Result.success(result);
    }

    @RequestMapping(value = "/ons/topic/1")
    @ResponseBody
    public Result onsTopic1() {
        Boolean result = ons.send("topic1", "ons test");
        return Result.success(result);
    }

    @RequestMapping(value = "/ons/topic/2")
    @ResponseBody
    public Result onsTopic2() {
        TwoObject obj = new TwoObject();
        obj.setContent("你好 ons");
        obj.setTitle("test");
        Boolean result = ons.send("topic2", obj);
        return Result.success(result);
    }


    @RequestMapping(value = "/http/ok")
    @ResponseBody
    public Result okHttp() throws Exception {
        String url = "https://www.baidu.com/";
        RequestData requestData = new RequestData(url);
        String data = okHttp.go(requestData);
        return Result.success(data);
    }

    @RequestMapping(value = "/http/my")
    @ResponseBody
    public Result myHttp() throws Exception {
        String url = "http://www.baidu.com";
        RequestData requestData = new RequestData(url).setRetryNumAndTime(2, 100).setTimeout(100,100);
        TestVO data = myHttp.go(requestData, TestVO.class);
        return Result.success(data);
    }

    @RequestMapping(value = "/http/rest")
    @ResponseBody
    public Result restHttp() throws Exception {
        String url = "https://www.baidu.com";
        RequestData requestData = new RequestData(url).setPostJson("{}");
        String data = restHttp.go(requestData);
        return Result.success(data);
    }

    @RequestMapping(value = "/cache/jedis")
    @ResponseBody
    public Result jedis() throws Exception {
        TwoObject obj = new TwoObject();
        obj.setContent("你好");
        obj.setTitle("test");
        jedis.setObj("test", obj, 100);
        TwoObject obj2 = jedis.getObj("test", TwoObject.class);
        return Result.success(obj2);
    }

    @RequestMapping(value = "/cache/redisson")
    @ResponseBody
    public Result redisson() throws Exception {
        TwoObject obj = new TwoObject();
        obj.setContent("你好2");
        obj.setTitle("tes2t");
        redisson.setObj("test2", obj, 100);
        TwoObject obj2 = redisson.getObj("test2");
        return Result.success(obj2);
    }

    @RequestMapping(value = "/dubbo/hello")
    @ResponseBody
    public Result dubbo(){
        String data = demoConsumerService.hello();
        return Result.success(data);
    }

    @RequestMapping(value = "/dubbo/consumer")
    @ResponseBody
    public Result dubboConsumer(){
        String zkAddress = "localhost:2181?timeout=10000";
        String service = "com.gandong8.trace.biz.service.DemoProviderService";
        ZKCuratorUtil.clientInit(zkAddress, service);
        return Result.success(ZKCuratorUtil.getConsumerIpList());
    }

    @RequestMapping(value = "/dubbo/provider")
    @ResponseBody
    public Result dubboProvider(){
        String zkAddress = "localhost:2181?timeout=10000";
        String service = "com.gandong8.trace.biz.service.DemoProviderService";
        ZKCuratorUtil.clientInit(zkAddress, service);
        return Result.success(ZKCuratorUtil.getProviderIpList());
    }

    @RequestMapping(value = "/thread/trace")
    @ResponseBody
    public Result threadTrace(){
        Executor executor = ThreadUtil.singleExecutor();
        executor.execute(this::test);
        return Result.success();
    }





}
